package com.wzx.ptplatform.controller.admin.set;

import com.wzx.ptplatform.domain.City;
import com.wzx.ptplatform.domain.Province;
import com.wzx.ptplatform.service.CityService;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员其他模块——市级管理")
@RestController
public class AdminSetCityController {

    final CityService cityService;

    public AdminSetCityController(CityService cityService) {
        this.cityService = cityService;
    }

    @RequestMapping("admin_set_city_message_list")
    public LayuiPager<City> adminSetCityMessageList(
            String cityName, String provinceName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        City city = new City();
        if (cityName != null && !"".equals(cityName)) {
            city.setCityName(cityName);
        }
        if (provinceName != null && !ConstUtils.PLEASE_CHOOSE.equals(provinceName)) {
            city.setProvince(new Province(Long.parseLong(provinceName)));
        }
        Page<City> pager = cityService.findList(city, page - 1, limit);
        LayuiPager<City> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_set_city_message_addView")
    public ModelAndView adminSetCityMessageAddView() {
        return new ModelAndView("admin/set/city_message_add");
    }

    @GetMapping("admin_set_city_message_editView")
    public ModelAndView adminSetCityMessageEditView(String id, Model model) {
        model.addAttribute("city", cityService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/set/city_message_edit");
    }

    @PostMapping("admin_set_city_message_save_or_edit")
    public Map<String, Object> adminSetProvinceMessageSaveOrEdit(City city) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (cityService.saveOrEdit(city)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_set_city_message_delete")
    public Map<String, Object> adminSetProvinceMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (cityService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_set_city_message_delete_select")
    public Map<String, Object> adminSetProvinceMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    cityService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
